﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;

using FinalProject.Web.IoC;

using FinalProject.Data.DBInteractions.Interfaces;
using FinalProject.Data.DBInteractions;

using FinalProject.Data.EntityRepositories.Interfaces;
using FinalProject.Data.EntityRepositories;

using FinalProject.Services.Interfaces;
using FinalProject.Services.Services;
using System.Data.Entity;
using FinalProject.Data;
using System.Web.Security;
using FinalProject.Web.Mappings;

namespace FinalProject.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            IUnityContainer container = GetUnityContainer();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            AutoMapperConfiguration.Configure();
        }
        private IUnityContainer GetUnityContainer()
        {
            IUnityContainer container = new UnityContainer()
            .RegisterType<IDBFactory, DBFactory>(new HttpContextLifetimeManager<IDBFactory>())
            .RegisterType<IUnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>())
            .RegisterType<IChargeCategoryService, ChargeCategoryService>(new HttpContextLifetimeManager<IChargeCategoryService>())
            .RegisterType<IChargeCategoryRepository, ChargeCategoryRepository>(new HttpContextLifetimeManager<IChargeCategoryRepository>())
            .RegisterType<IClientService, ClientService>(new HttpContextLifetimeManager<IClientService>())
            .RegisterType<IClientRepository, ClientRepository>(new HttpContextLifetimeManager<IClientRepository>())
            .RegisterType<IUserService, UserService>(new HttpContextLifetimeManager<IUserService>())
            .RegisterType<IUserRepository, UserRepository>(new HttpContextLifetimeManager<IUserRepository>())
            .RegisterType<IUserRoleService, UserRoleService>(new HttpContextLifetimeManager<IUserRoleService>())
            .RegisterType<IUserRoleRepository, UserRoleRepository>(new HttpContextLifetimeManager<IUserRoleRepository>())
            .RegisterType<ICompanyService, CompanyService>(new HttpContextLifetimeManager<ICompanyService>())
            .RegisterType<ICompanyRepository, CompanyRepository>(new HttpContextLifetimeManager<ICompanyRepository>())
            .RegisterType<IStoreService, StoreService>(new HttpContextLifetimeManager<IStoreService>())
            .RegisterType<IStoreRepository, StoreRepository>(new HttpContextLifetimeManager<StoreRepository>())
            .RegisterType<IUserDetailsService, UserDetailsService>(new HttpContextLifetimeManager<UserDetailsService>())
            .RegisterType<IUserDetailsRepository, UserDetailsRepository>(new HttpContextLifetimeManager<UserDetailsRepository>())
            .RegisterType<ITicketService, TicketService>(new HttpContextLifetimeManager<ITicketService>())
            .RegisterType<ITicketRepository, TicketRepository>(new HttpContextLifetimeManager<ITicketRepository>())
            .RegisterType<IItemService, ItemService>(new HttpContextLifetimeManager<IItemService>())
            .RegisterType<IItemRepository, ItemRepository>(new HttpContextLifetimeManager<IItemRepository>())
            .RegisterType<ICustomMembershipProvider, CustomMembershipProvider>(new HttpContextLifetimeManager<ICustomMembershipProvider>())
            .RegisterType<ICustomRoleProvider, CustomRoleProvider>(new HttpContextLifetimeManager<ICustomRoleProvider>());

            return container;
        }
    }
}